<import file="block/common.html" name="url"  :tpl_cfg="['urlName'=>'用户组栏目权限','backList'=>'返回列表']"/>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <blockquote class="layui-elem-quote">当前角色：<b style="color: red;">
                            <{$info['groupname']}>
                        </b> (超级管理员组不受栏目权限限制)</blockquote>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-form">
        <form class="layui-form" action="">
            <fieldset class="layui-elem-field">
                <legend>栏目权限管理</legend>
                <div class="layui-field-box">
                    <div class="layui-form-item">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-block">
                            <div id="contentCateTree" class="cxuu-tree-channal" data-id="0"></div>

                        </div>
                    </div>
                </div>
            </fieldset>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn">
                        <?php echo lang('save');?>
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.define(function () {
        var form = layui.form,
            layer = layui.layer,
            tree = layui.tree,
            $ = layui.jquery;

        $.getJSON("<{CAUrl('contentcate','index')}>&mod=1&list_json", function (res) { //?role=1  时，带权限判断的栏目组
            tree.render({
                elem: '#contentCateTree',
                data: res.data,
                id: "treeID",
                showCheckbox: true,
                //click: function (res) {
                //    // console.log("obj", res.data.id);
                //    // $("#main").load("/Admin/Content/Index?cid=" + res.data.id);} ,

                //oncheck: function (obj) {
                //      checkData = tree.getChecked('cid'); //获取选中节点的数据
                //     console.log("cid:",checkData)
                // }
            });
            let channlsetid = "<{$info['channlid']}>";
            //栏目选中
            tree.setChecked('treeID', channlsetid.split(','));
        })

      let getCheckedId = function(data) {
        var id = "";
        $.each(data, function (index, item) {
            if (id != "") {
                id = id + "," + item.id;
            }
            else {
                id = item.id;
            }
            //item 没有children属性
            if (item.children != null) {
                var i = getCheckedId(item.children);
                if (i != "") {
                    id = id + "," + i;
                }
            }
        });
        return id;
    }

        //监听提交
        form.on('submit(saveBtn)', function () {
            let checkData =  getCheckedId(tree.getChecked('treeID')).split(',');
            $.post('<{CAUrl("admingroup","edit")}>&channlrolepost', { id: '<{$info["id"]}>', channlrole: checkData }, function (res) {
                if (res.status == 1) {
                    layer.msg(res.info, { icon: 1, time: 1000 }, function () {
                        loadMain('<{CAUrl("admingroup","index")}>');
                    });
                } else {
                    layer.msg(res.info, { icon: 2, anim: 6, time: 5000 });
                }
            });
            return false;
        });
    });

    

</script>